﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Caching;
using Mozlite.Caching;

namespace Mozlite
{
    /// <summary>
    /// 缓存管理类。
    /// </summary>
    public class Caches
    {
        #region remove
        /// <summary>
        /// 移除指定项目。
        /// </summary>
        /// <param name="key">缓存键。</param>
        public static void Remove(string key)
        {
            Cacher.Remove(key);
        }

        /// <summary>
        /// 清楚所有缓存。
        /// </summary>
        public static void Clear()
        {
            Cacher.Clear();
        }

        /// <summary>
        /// 通过正则表达式移除缓存。
        /// </summary>
        /// <param name="pattern">缓存键正则表达式。</param>
        public static void RemoveByPattern(string pattern)
        {
            Cacher.RemoveByPattern(pattern);
        }
        #endregion

        #region insert
        /// <summary>
        /// 插入缓存对象，相对过期时间（没有使用当前对象开始计时）为3分钟。
        /// </summary>
        /// <param name="key">缓存键。</param>
        /// <param name="obj">缓存对象。</param>
        /// <param name="dep">缓存依赖项。</param>
        public static void Insert(string key, object obj, CacheDependency dep = null)
        {
            Cacher.Insert(key, obj, dep);
        }
        /// <summary>
        /// 插入缓存对象。
        /// </summary>
        /// <param name="key">缓存键。</param>
        /// <param name="obj">缓存对象。</param>
        /// <param name="seconds">缓存时间。</param>
        /// <param name="priority">缓存优先级。</param>
        public static void Insert(string key, object obj, int seconds, CacheItemPriority priority = CacheItemPriority.Normal)
        {
            Cacher.Insert(key, obj, seconds, priority);
        }
        /// <summary>
        /// 插入缓存对象。
        /// </summary>
        /// <param name="key">缓存键。</param>
        /// <param name="obj">缓存对象。</param>
        /// <param name="dep">缓存依赖项。</param>
        /// <param name="seconds">缓存时间。</param>
        /// <param name="priority">缓存优先级。</param>
        /// <param name="onRemoveCallback">当缓存项移除时执行的方法。</param>
        public static void Insert(string key, object obj, CacheDependency dep, int seconds, CacheItemPriority priority = CacheItemPriority.Normal, CacheItemRemovedCallback onRemoveCallback = null)
        {
            Cacher.Insert(key, obj, dep, seconds, priority, onRemoveCallback);
        }
        /// <summary>
        /// 永久性插入缓存。
        /// </summary>
        /// <param name="key">缓存键。</param>
        /// <param name="obj">缓存对象实例。</param>
        /// <param name="dep">缓存依赖项。</param>
        public static void Max(string key, object obj, CacheDependency dep = null)
        {
            Cacher.Max(key, obj, dep);
        }
        /// <summary>
        /// 永久性插入不可移除缓存。
        /// </summary>
        /// <param name="key">缓存键。</param>
        /// <param name="obj">缓存对象实例。</param>
        /// <param name="dep">缓存依赖项。</param>
        public static void Permanent(string key, object obj, CacheDependency dep = null)
        {
            Cacher.Permanent(key, obj, dep);
        }
        #endregion

        #region get
        /// <summary>
        /// 获取缓存对象。
        /// </summary>
        /// <returns>返回缓存对象。</returns>
        public static object Get(string key)
        {
            return Cacher.Get(key);
        }

        /// <summary>
        /// 获取缓存对象。
        /// </summary>
        /// <typeparam name="T">缓存类。</typeparam>
        /// <param name="key">缓存键。</param>
        /// <param name="loader">从数据提供者中载入实例对象的方法。</param>
        /// <returns>返回缓存对象。</returns>
        public static T Get<T>(string key, CacheLoaderDelegate loader)
        {
            return Cacher.Get<T>(key, loader);
        }

        /// <summary>
        /// 获取缓存对象。
        /// </summary>
        /// <typeparam name="T">缓存类。</typeparam>
        /// <param name="key">缓存键。</param>
        /// <returns>返回缓存对象。</returns>
        /// <remarks>使用此方法需要注意，必须已经确定缓存中具有该键的缓存项，否则将抛出错误！</remarks>
        public static T Get<T>(string key) where T : class
        {
            return Cacher.Get<T>(key);
        }
        /// <summary>
        /// 获取缓存实例对象。
        /// </summary>
        /// <typeparam name="T">缓存类。</typeparam>
        /// <param name="key">缓存键。</param>
        /// <param name="refreshIntervalSeconds">检测时间间隔秒钟。</param>
        /// <param name="slidingExpirationSeconds">相对过期时间秒钟。</param>
        /// <param name="loaderDelegate">缓存加载器。</param>
        /// <param name="priority">缓存优先级。</param>
        /// <returns>返回缓存对象。</returns>
        public static T Get<T>(string key, int refreshIntervalSeconds, int slidingExpirationSeconds, CacheLoaderDelegate loaderDelegate, CacheItemPriority priority = CacheItemPriority.Normal) where T : class
        {
            return Cacher.Get<T>(key, refreshIntervalSeconds, slidingExpirationSeconds, loaderDelegate, priority);
        }
        /// <summary>
        /// 获取缓存实例对象。
        /// </summary>
        /// <typeparam name="T">缓存类。</typeparam>
        /// <param name="key">缓存键。</param>
        /// <param name="refreshInterval">检测时间间隔刻度。</param>
        /// <param name="slidingExpiration">相对过期时间刻度。</param>
        /// <param name="loaderDelegate">缓存加载器。</param>
        /// <param name="priority">缓存优先级。</param>
        /// <returns>返回缓存对象。</returns>
        public static T Get<T>(string key, TimeSpan refreshInterval, TimeSpan slidingExpiration, CacheLoaderDelegate loaderDelegate, CacheItemPriority priority = CacheItemPriority.Normal) where T : class
        {
            return Cacher.Get<T>(key, refreshInterval, slidingExpiration, loaderDelegate, priority);
        }
        #endregion
    }
}
